package com.zhoug.demo.sendpush.http;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Retrofit;

/**
 * @description: 类的简要描述
 * @author: 35574
 * @date: 2025/11/4 16:42
 */
public class HttpClient {
    private static final String TAG = ">>>HttpClient";
    private static boolean debug = true;
    private Retrofit retrofit;
    private static HttpClient instance;
    private Map<Class<?>, Object> requests = new HashMap<>();

    protected HttpClient() {
        init();
    }

    public static HttpClient getInstance() {
        if (instance == null) {
            synchronized (HttpClient.class) {
                if (instance == null) {
                    instance = new HttpClient();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化
     */
    private void init() {
        MyHttpLogInterceptor logInterceptor = new MyHttpLogInterceptor();
        if (debug) {
            logInterceptor.setLevel(MyHttpLogInterceptor.Level.BODY);
        } else {
            logInterceptor.setLevel(MyHttpLogInterceptor.Level.NONE);
        }


        retrofit = new MyRetrofitBuilder()
                .setDebug(debug)
                .setDebugLevel(null)
                .setConnectTimeOut(30)
                .setReadTimeOut(120)
                .setWriteTimeOut(120)
                .addInterceptor(logInterceptor)
                .build("http://192.168.0.224:8005/api/");

    }

    /**
     * BaseRequests请求接口
     *
     * @return
     */
    public BaseRequests getBaseRequests() {
        return getRequest(BaseRequests.class);
    }

    /**
     * 创建请求接口
     *
     * @param cls
     * @param <T>
     * @return
     * @noinspection unchecked
     */
    public <T> T getRequest(Class<T> cls) {
        if (retrofit == null) {
            init();
        }
        Object object = requests.get(cls);
        if (object != null) {
            return (T) object;
        } else {
            T t = retrofit.create(cls);
            requests.put(cls, t);
            return t;
        }
    }


}

